<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./node_modules/vue/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    <ul>

      <li v-for="item in products">
        <span>{{item.name}} -- {{item.count}}</span>
        <span v-show="item.count===0">没有了</span>
        <button @click="item.count++">add</button>
        <input type="text" v-model.number="item.count">
        <button @click="item.count>0 ? --item.count :0">sub</button>
      </li>
    </ul>
    <p>合计：{{total}}</p>
  </div>

  <script>
    const vm = new Vue({
      el: "#app",
      data: {
        products: [
          { name: "苹果", count: 0 },
          { name: "香蕉", count: 3 },
          { name: "鸭梨", count: 4 },
        ]
      },
      computed: {
        total() {
          // 计算加和
          return this.products.reduce((total, item) => {
            return total + item.count
          }, 0)
        }
      }
    })
  </script>
</body>

</html>
